package com.sunfield.windpower.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.sunfield.windpower.R;
import com.sunfield.windpower.bean.ContactInfo;
import com.sunfield.windpower.bean.DepartmentInfo;

import org.androidannotations.annotations.EBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wxl on 2017/2/8.
 */
@EBean
public class ContactsAdapter extends RecyclerView.Adapter {

    public static final int TYPE_SEARCH = 1;
    public static final int TYPE_DEPARTMENT = 2;
    public static final int TYPE_LABEL = 3;
    public static final int TYPE_CONTACT = 4;

    List<DepartmentInfo> departmentList;
    List<ContactInfo> contactList;

    Context context;

    public ContactsAdapter(Context context) {
        this.context = context;
        departmentList = new ArrayList<>();
        contactList = new ArrayList<>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        View view;
        switch (viewType) {
            case TYPE_SEARCH:
                view = LayoutInflater.from(context).inflate(R.layout.item_contact_search, parent, false);
                viewHolder = new SearchHolder(view);
                break;
            case TYPE_DEPARTMENT:
                view = LayoutInflater.from(context).inflate(R.layout.item_contact_department, parent, false);
                viewHolder = new DepartmentHolder(view);
                break;
            case TYPE_LABEL:
                view = LayoutInflater.from(context).inflate(R.layout.item_contact_label, parent, false);
                viewHolder = new LabelHolder(view);
                break;
            case TYPE_CONTACT:
                view = LayoutInflater.from(context).inflate(R.layout.item_contact_contact, parent, false);
                viewHolder = new ContactHolder(view);
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof SearchHolder) {
            ((SearchHolder) holder).fl_search.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //TODO 搜索
                }
            });
        } else if (holder instanceof DepartmentHolder) {
            DepartmentInfo departmentInfo = departmentList.get(getTypePosition(position));
            ((DepartmentHolder) holder).tv_name.setText(departmentInfo.getName());
            ((DepartmentHolder) holder).tv_number.setText(departmentInfo.getCount());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //TODO 跳转
                }
            });
        } else if (holder instanceof LabelHolder) {
            ((LabelHolder) holder).iv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    contactList.clear();
                    notifyDataSetChanged();
                }
            });
        } else if (holder instanceof ContactHolder) {
            ContactInfo contactInfo = contactList.get(getTypePosition(position));
            ((ContactHolder) holder).tv_name.setText(contactInfo.getName());
            ((ContactHolder) holder).tv_email.setText(contactInfo.getEmail());
            ((ContactHolder) holder).tv_label.setText("(" + contactInfo.getHonor() + ")");
            ((ContactHolder) holder).tv_label.setVisibility(TextUtils.isEmpty(contactInfo.getHonor()) ? View.INVISIBLE : View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return 2 + departmentList.size() + contactList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_SEARCH;
        } else if (position < 1 + departmentList.size()) {
            return TYPE_DEPARTMENT;
        } else if (position == 1 + departmentList.size()) {
            return TYPE_LABEL;
        }
        return TYPE_CONTACT;
    }

    /**
     * 在当前type中的序列
     *
     * @param position
     * @return
     */
    public int getTypePosition(int position) {
        int index = 0;
        switch (getItemViewType(position)) {
            case TYPE_SEARCH:
                index = 0;
                break;
            case TYPE_DEPARTMENT:
                index = position - 1;
                break;
            case TYPE_LABEL:
                index = position - 1 - departmentList.size();
                break;
            case TYPE_CONTACT:
                index = position - 2 - departmentList.size();
                break;
        }
        return index;
    }

    /**
     * 当前type的长度
     *
     * @param position
     * @return
     */
    public int getTypeSize(int position) {
        int size = 0;
        switch (getItemViewType(position)) {
            case TYPE_SEARCH:
                size = 1;
                break;
            case TYPE_DEPARTMENT:
                size = departmentList.size();
                break;
            case TYPE_LABEL:
                size = 1;
                break;
            case TYPE_CONTACT:
                size = contactList.size();
                break;
        }
        return size;
    }

    public List<DepartmentInfo> getDepartmentList() {
        return departmentList;
    }

    public void setDepartmentList(List<DepartmentInfo> departmentList) {
        this.departmentList.clear();
        if (departmentList != null) {
            this.departmentList.addAll(departmentList);
        }
        notifyDataSetChanged();
    }

    public List<ContactInfo> getContactList() {
        return contactList;
    }

    public void setContactList(List<ContactInfo> contactList) {
        this.contactList.clear();
        if (contactList != null) {
            this.contactList.addAll(contactList);
        }
        notifyDataSetChanged();
    }

    public class SearchHolder extends RecyclerView.ViewHolder {
        public FrameLayout fl_search;

        public SearchHolder(View rootView) {
            super(rootView);
            this.fl_search = (FrameLayout) rootView.findViewById(R.id.fl_search);
        }
    }

    public class DepartmentHolder extends RecyclerView.ViewHolder {
        public TextView tv_name;
        public TextView tv_number;

        public DepartmentHolder(View rootView) {
            super(rootView);
            this.tv_name = (TextView) rootView.findViewById(R.id.tv_name);
            this.tv_number = (TextView) rootView.findViewById(R.id.tv_number);
        }

    }

    public class LabelHolder extends RecyclerView.ViewHolder {
        public ImageView iv_delete;

        public LabelHolder(View rootView) {
            super(rootView);
            this.iv_delete = (ImageView) rootView.findViewById(R.id.iv_delete);
        }

    }

    public class ContactHolder extends RecyclerView.ViewHolder {
        public ImageView iv_head;
        public TextView tv_name;
        public TextView tv_label;
        public TextView tv_email;

        public ContactHolder(View rootView) {
            super(rootView);
            this.iv_head = (ImageView) rootView.findViewById(R.id.iv_head);
            this.tv_name = (TextView) rootView.findViewById(R.id.tv_name);
            this.tv_label = (TextView) rootView.findViewById(R.id.tv_label);
            this.tv_email = (TextView) rootView.findViewById(R.id.tv_email);
        }

    }
}
